<template>
  <window ref="win" title="选择优惠">
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-body am-fr" style="padding: 0px ;min-height: 450px">
          <!--searching condition-->
          <div class="am-u-sm-12 am-form ">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
              <el-form-item label="优惠类型" required>
                <el-select v-model="ruleForm.discountId" placeholder="请选择优惠类型">
                  <el-option v-for="item in discounts" :label="item.name" :disabled="item.disabled" :value="item.discountId"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item v-if="ruleForm.discountId === 0" required label="优惠金额">
                <el-input v-model="ruleForm.discountAmount" placeholder="请录入优惠金额"></el-input>
              </el-form-item>
              <el-form-item v-if="ruleForm.discountId === 1" required label="现金优惠码">
                <el-input v-model="ruleForm.discountCode" placeholder="请录入优惠码"></el-input>
              </el-form-item>
              <el-form-item v-if="ruleForm.discountId === 2" required label="优惠口令">
                <el-input v-model="ruleForm.discountCode" placeholder="优惠口令由内部员工生成，请确保优惠口令准确性！"></el-input>
              </el-form-item>
              <el-form-item v-if="ruleForm.discountId === 3" required label="优惠码">
                <el-input v-model="ruleForm.discountCode" placeholder="优惠码由电子优惠券系统生成，请确保优惠口令准确性！"></el-input>
              </el-form-item>
              <el-form-item v-if="inputParams.length" required v-for="item in inputParams" :label="item.name">
                <el-input v-model="item.value" :placeholder="item.remark || ''"></el-input>
              </el-form-item>
              <el-form-item v-if="ruleForm.discountId && ruleForm.discountId !== 0 && ruleForm.discountId !== 1" label="相关附件">
                <file-upload @uploaded="handleUpload">
                  <div>{{ruleForm.attachmentName}}</div>
                </file-upload>
                <el-button size="small" style="position: absolute; top: 6px;left: 172px;" @click="handleDelAttachment">删除附件</el-button>
              </el-form-item>
            </el-form>
            <div>
              <el-button @click="handleConfirm" type="success">确认</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </window>
</template>

<script>
  import io from '../../lib/io'

  export default{
    data: function () {
      return {
        discounts: [],
        ruleForm: {
          discountId: '',
          discountAmount: '',
          discountCode: '',
          attachmentName: '',
          attachmentUrls: '',
        },
        rules: {},
        inputParams: [],
        disabledIds: [],
        areaTeamId: '',
        courseOrderId: '',
      }
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
    },
    watch: {
      'ruleForm.discountId' (newVal) {
        let params = ''
        let name = ''
        if (newVal !== '' && newVal !== undefined) {
          this.discounts.map(val => {
            if (newVal === val.discountId) {
              params = val.inputParams
              name = val.name
            }
          })
          this.inputParams = params || []
          this.inputParams.map(val => {
            val.value = ''
          })
          this.discountName = name
        }
      },
    },
    methods: {
      handleUpload(info) {
        this.ruleForm.attachmentUrls = info.url  + '?attname=' + info.original
        this.ruleForm.attachmentName = info.original
      },
      handleDelAttachment() {
        this.ruleForm.attachmentUrls = ''
        this.ruleForm.attachmentName = ''
      },
      loadDiscountOfPolicy:function(){
        io.post(io.apiAdminDiscountListOfPolicy, {areaTeamId: this.areaTeamId},
          (ret) =>{
            if (ret.success) {
              this.discounts = ret.data
              this.discounts.map(discount => {
                if (discount.inputParams) {
                  let inputParams = JSON.parse(discount.inputParams)
                  inputParams.map(val => {
                    val.value = ''
                  })
                  discount.inputParams = inputParams
                }
              })
              this.discounts.unshift({
                discountId: 0,
                name: '特殊优惠',
                inputParams: [{name: '优惠描述',pattern: '', remark: '请输入优惠描述'}]
              })
              this.discounts.unshift({
                discountId: 1,
                name: '优惠券码(誉优)',
              })
              this.discounts.unshift({
                discountId: 3,
                name: '优惠券码(卓越)',
              })

              this.discounts.unshift({
                discountId: 2,
                name: '内部员工优惠',
              })

              this.disabledIds.map(disabledId => {
                this.discounts.map(val => {
                  if (Number(val.discountId) === Number(disabledId)) {
                    val.disabled = true
                  }
                })
              })
            } else {
              this.$alert(ret.desc || '加载政策优惠失败')
            }
          })
      },
      handleConfirm () {
        let params = {courseOrderId: this.courseOrderId}
        if (this.ruleForm.discountId === 0) {
          params.discountId  = this.ruleForm.discountId
          params.discountAmount = this.ruleForm.discountAmount
          params.inputParams  = JSON.stringify(this.inputParams)
          params.type = 1
        } else if (this.ruleForm.discountId === 1) {
          params.discountId  = this.ruleForm.discountId
          params.discountCode  = this.ruleForm.discountCode
          params.inputParams  = JSON.stringify(this.inputParams)
          params.type = 2
        } else if (this.ruleForm.discountId === 2) {
          params.discountId  = this.ruleForm.discountId
          params.discountCode  = this.ruleForm.discountCode
          params.inputParams  = JSON.stringify(this.inputParams)
          params.type = 3
        }  else if (this.ruleForm.discountId === 3) {
          params.discountId  = this.ruleForm.discountId
          params.discountCode  = this.ruleForm.discountCode
          params.inputParams  = JSON.stringify(this.inputParams)
          params.type = 3
        }else {
          params.discountId  = this.ruleForm.discountId
          params.attachmentUrls  = this.ruleForm.attachmentUrls
          params.inputParams  = JSON.stringify(this.inputParams)
          params.type = 0
        }
        if (this.validate(params)) {
          io.post(io.addCourseOrderDiscount, params, (ret) => {
            if (ret.success) {
              this.getCourseOrderDiscount()
            } else {
              this.$alert(ret.desc)
            }
          })
        }
      },
      validate (params) {
        if (this.ruleForm.discountId === '' || this.ruleForm.discountId === undefined) {
          this.$alert('请选择优惠类型')
          return false
        }
        if (this.ruleForm.discountId === 0) {
          if (!params.discountAmount) {
            this.$alert('请填写优惠金额')
            return false
          } else if (isNaN(Number(params.discountAmount))){
            this.$alert('优惠金额需要填写数字')
            return false
          }
        } else if (this.ruleForm.discountId === 1) {
          if (!params.discountCode) {
            this.$alert('请填写优惠码')
            return false
          }
        } else if (this.ruleForm.discountId === 2) {
          if (!params.discountCode) {
            this.$alert('请填写优惠口令')
            return false
          }
        }

        if (this.inputParams.length) {
          let err = false
          for (let i =0,len = this.inputParams.length; i < len; i++) {
            if (!this.inputParams[i].value) {
              this.$alert(this.inputParams[i].name + '不能为空')
              err = true
              break
            }
            if (!new RegExp(this.inputParams[i].pattern).test(this.inputParams[i].value)) {
              this.$alert('请填写正确的' + this.inputParams[i].name)
              err = true
              break
            }
          }
          return !err
        }
        return true
      },
      getCourseOrderDiscount () {
        io.post(io.courseOrderDiscount, {
          courseOrderId: this.courseOrderId
        }, (ret) => {
          if (ret.success) {
            this.$emit('ok',ret.data);
            this.$refs.win.close()
          } else {
            this.$alert(ret.desc)
          }
        })
      },
      show:function(){
        this.loadDiscountOfPolicy()
        this.$refs.win.show({
          width:1000,
          height:600
        })
      }
    }
  }
</script>
